# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
pkgname=mongo-c-driver
pkgver=1.27.0
pkgrel=0
pkgdesc="Client library written in C for MongoDB"
url="https://github.com/mongodb/mongo-c-driver"
arch="all"
license="Apache-2.0"
makedepends="
	cmake
	libtool
	openssl-dev>3
	py3-sphinx
	samurai
	snappy-dev
	zlib-dev
	zstd-dev
	"
subpackages="
	$pkgname-static:_static
	libbson-static:_libbson_static
	$pkgname-dev:_dev
	$pkgname-doc
	libbson
	libbson-dev
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/mongodb/mongo-c-driver/archive/refs/tags/$pkgver.tar.gz"
options="!check" # missing cmake file on running test (fixme)

# secfixes:
#   1.25.4-r0:
#     - CVE-2023-0437

build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_VERSION="$pkgver" \
		-DENABLE_BSON:STRING=ON \
		-DENABLE_MONGOC=ON \
		-DENABLE_SSL=OPENSSL \
		-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF \
		-DENABLE_MAN_PAGES=ON \
		-DENABLE_TESTS=ON \
		-DENABLE_EXAMPLES=OFF \
		-DCMAKE_SKIP_RPATH=ON
	cmake --build build
}

check() {
	export MONGOC_TEST_SKIP_MOCK=on
	export MONGOC_TEST_SKIP_SLOW=on
	export MONGOC_TEST_SKIP_LIVE=on

	ninja -C build test
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# removes COPYING, NEWS, README, uninstall script
	rm -rf "$pkgdir/usr/share/mongo-c-driver"
}

libbson() {
	pkgdesc="Building, parsing, and iterating BSON documents"

	amove usr/lib/libbson-*.so.*
}

_static() {
	# Depend on the dev package that has the headers required
	# for compiling against, whether it is static or shared
	depends_static="mongo-c-driver-dev=$pkgver-r$pkgrel"

	default_static

	# Move the libbson static archive from the mongo-c-driver-static
	# back to $pkgdir it will be then moved to libbson-static
	mkdir -p "$pkgdir"/usr/lib
	mv "$subpkgdir"/usr/lib/libbson-static-1.0.a "$pkgdir"/usr/lib

	# Move development dependencies that only make sense in the context
	# of static libraries like libmongoc-static-1.0.pc
	mkdir -p "$subpkgdir"/usr/lib/pkgconfig
	mkdir -p "$subpkgdir"/usr/lib/cmake
	mv "$pkgdir"/usr/lib/pkgconfig/libmongoc-static-1.0.pc "$subpkgdir"/usr/lib/pkgconfig
	mv "$pkgdir"/usr/lib/cmake/libmongoc-static-1.0 "$subpkgdir"/usr/lib/cmake
}

_libbson_static() {
	# Depend on the dev package that has the headers required
	# for compiling against, whether it is static or shared
	depends_static="libbson-dev=$pkgver-r$pkgrel"

	default_static

	# Move development dependencies that only make sense in the context
	# of static libraries like libbson-static-1.0.pc
	mkdir -p "$subpkgdir"/usr/lib/pkgconfig
	mkdir -p "$subpkgdir"/usr/lib/cmake
	mv "$pkgdir"/usr/lib/pkgconfig/libbson-static-1.0.pc "$subpkgdir"/usr/lib/pkgconfig
	mv "$pkgdir"/usr/lib/cmake/libbson-static-1.0 "$subpkgdir"/usr/lib/cmake
}

_dev() {
	# This should be removed once abuild knows how to search for pkg-config
	# files that are in another subpackage that is also being created alongside
	# this one.
	depends_dev="libbson-dev"

	default_dev

	# libbson pkg-config files
	mkdir -p "$pkgdir"/usr/lib/pkgconfig
	mv "$subpkgdir"/usr/lib/pkgconfig/libbson-1.0.pc "$pkgdir"/usr/lib/pkgconfig

	# libbson solink
	mkdir -p "$pkgdir"/usr/lib
	mv "$subpkgdir"/usr/lib/libbson-1.0.so "$pkgdir"/usr/lib

	# libbson cmake file
	mkdir -p "$pkgdir"/usr/lib/cmake
	mv "$subpkgdir"/usr/lib/cmake/bson-1.0 "$pkgdir"/usr/lib/cmake
	mv "$subpkgdir"/usr/lib/cmake/libbson-1.0 "$pkgdir"/usr/lib/cmake

	# libbson include files
	mkdir -p "$pkgdir"/usr/include
	mv "$subpkgdir"/usr/include/libbson-1.0 "$pkgdir"/usr/include
}

sha512sums="
547caacbff9ff43788c658743825ee16ae13e75f9322b0fcd8e107985f9d043a3cb133893ea870c2e5e2c92bc13a9cb69d9a102603f8fa3deb3f2fe26a6f8432  mongo-c-driver-1.27.0.tar.gz
"
